import argparse
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
import pandas as pd
from tqdm import tqdm
from csv import DictWriter
import sys
import json
import os

parser = argparse.ArgumentParser()
parser.add_argument("-H","--host", default="127.0.0.1:9200")
parser.add_argument("-i","--index", nargs="*",default="test")
parser.add_argument("-o","--output", default=sys.stdout)
args = parser.parse_args()

print(args.host)
client = Elasticsearch(hosts=args.host.strip())
for i in args.index:
    s = Search(using=client, index=i.strip())
    pro = tqdm(total=s.count())
    output = os.path.join("/tmp", i+".json")
    with open(output, "w") as fp:
        c = 0
        for hit in s.scan():
            c+=1
            if c > 1000:
                break
            d = json.dumps(hit.to_dict())
            fp.write(d + "\n")
            pro.update()
            

    


